import { db } from '../index';
import { settings } from '../schema';
import { eq } from 'drizzle-orm';
import { createId } from '@paralleldrive/cuid2';

/**
 * 系统设置种子数据
 */
export async function runSettingSeeds() {
  try {
    console.log('🔄 创建系统设置种子数据...');
    
    const defaultSettings = [
      {
        id: createId(),
        key: 'site_name',
        value: 'CX-AD 创新广告',
        description: '网站名称',
        type: 'string',
        category: 'general',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'site_description',
        value: '专业的广告标识制作服务商，提供发光字、LED显示屏、门头招牌等产品',
        description: '网站描述',
        type: 'string',
        category: 'general',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'site_keywords',
        value: '广告标识,发光字,LED显示屏,门头招牌,标识制作',
        description: '网站关键词',
        type: 'string',
        category: 'seo',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'contact_phone',
        value: '0760-88888888',
        description: '联系电话',
        type: 'string',
        category: 'contact',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'contact_email',
        value: 'info@cx-ad.com',
        description: '联系邮箱',
        type: 'string',
        category: 'contact',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'contact_address',
        value: '广东省中山市东区起湾道3号',
        description: '联系地址',
        type: 'string',
        category: 'contact',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'business_hours',
        value: '周一至周六 8:00-18:00',
        description: '营业时间',
        type: 'string',
        category: 'contact',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'company_intro',
        value: 'CX-AD创新广告成立于2010年，专注于广告标识行业13年，是中山地区领先的广告制作服务商。',
        description: '公司简介',
        type: 'string',
        category: 'about',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'wechat_official',
        value: 'cx-ad-2024',
        description: '微信公众号',
        type: 'string',
        category: 'social',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'qq_group',
        value: '123456789',
        description: 'QQ交流群',
        type: 'string',
        category: 'social',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'seo_title',
        value: 'CX-AD创新广告 - 专业广告标识制作服务商',
        description: 'SEO标题',
        type: 'string',
        category: 'seo',
        isPublic: 1
      },
      {
        id: createId(),
        key: 'upload_max_size',
        value: '10485760',
        description: '文件上传最大尺寸(字节)',
        type: 'number',
        category: 'upload',
        isPublic: 0
      },
      {
        id: createId(),
        key: 'upload_allowed_types',
        value: 'jpg,jpeg,png,gif,pdf,doc,docx',
        description: '允许上传的文件类型',
        type: 'string',
        category: 'upload',
        isPublic: 0
      }
    ];
    
    for (const setting of defaultSettings) {
      // 检查设置是否已存在
      const existing = await db.select().from(settings).where(eq(settings.key, setting.key)).limit(1);
      
      if (existing.length === 0) {
        await db.insert(settings).values(setting);
        console.log(`✅ 创建设置: ${setting.key}`);
      } else {
        console.log(`ℹ️ 设置 ${setting.key} 已存在，跳过创建`);
      }
    }
    
  } catch (error) {
    console.error('❌ 系统设置种子数据创建失败:', error);
    throw error;
  }
}